/**
 * 导入模块
 */
import { createRouter, createWebHistory } from 'vue-router';
import NProgress from 'nprogress';

/**
 * 导入类型声明
 */
import type {
  NavigationGuardNext,
  RouteLocationNormalizedGeneric,
  RouteLocationNormalizedLoadedGeneric,
  RouteRecordRaw,
  Router,
} from 'vue-router';

/**
 * 导入子模块
 */
import login from './modules/login';
import dashboard from './modules/dashboard';

/**
 * 获取路由配置
 * @returns
 */
function getRouterConf(): Array<RouteRecordRaw> {
  const routes: Array<RouteRecordRaw> = [
    {
      path: '/',
      redirect: '/layout',
    },
    login,
    {
      path: '/layout',
      name: 'layout',
      component: () => import('@/layouts/index.vue'),
      children: [dashboard],
    },
  ];
  return routes;
}

/**
 * 路由器
 */
const router: Router = createRouter({
  // history 模式
  history: createWebHistory('/'),
  // 路由模块
  routes: getRouterConf(),
});

/**
 * 路由钩子函数
 */
router.beforeEach(
  async (to: RouteLocationNormalizedGeneric, from: RouteLocationNormalizedLoadedGeneric, next: NavigationGuardNext) => {
    NProgress.configure({ showSpinner: false });
    NProgress.start();
    next();
  },
);

/**
 * 路由钩子函数
 */
router.afterEach(() => {
  NProgress.done();
});

/**
 * 导出路由配置模块
 */
export default router;
